package com.pshcp.service;

import com.framework.thread.ReqComHold;
import com.framework.util.SystemUtil;
import com.pshcp.common.enums.OnlineBizStatusEnum;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.domain.dto.DoctorOrderCountDTO;
import com.pshcp.domain.dto.PatientOnlineOrderDTO;
import com.pshcp.domain.model.OrderReg;
import com.pshcp.domain.model.PatientOnlineOrder;
import com.pshcp.mapper.PatientOnlineOrderMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 患者在线订单 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class PatientOnlineOrderService {

    @Autowired
    private PatientOnlineOrderMapper patientOnlineOrderMapper;

    public int doctorOnlineCount(Long doctorId){
        PatientOnlineOrderDTO patientOnlineOrderDTO = new PatientOnlineOrderDTO();
        patientOnlineOrderDTO.setDoctorId(doctorId);
        patientOnlineOrderDTO.setStatus(CommonStatusEnum.INIT.getCode());
        return listCount(patientOnlineOrderDTO);
    }

    /**
     * close online consult order.
     * @param id
     */
    public void closeOnlineOrder(Long id){
        PatientOnlineOrder onlineOrder = new PatientOnlineOrder();
        onlineOrder.setId(id);
        onlineOrder.setBizStatus(OnlineBizStatusEnum.CLOSE.getCode());
        this.update(onlineOrder);
    }

    /**
     * 医生被咨询数量
     * @param doctorIds
     * @return
     */
    public List<DoctorOrderCountDTO> countByDoctorIds(List<Long> doctorIds){
        return patientOnlineOrderMapper.countByDoctorIds(doctorIds);
    }

    /**
     * 退款中的订单
     * @return
     */
    public List<PatientOnlineOrder> refundingList(){
        Example example = new Example(PatientOnlineOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("payStatus", RegPayStatusEnum.REFUND_ING.getCode());
        example.setOrderByClause(" created_time limit 15");
        return patientOnlineOrderMapper.selectByExample(example);
    }

    public PageList<PatientOnlineOrder> list(PatientOnlineOrderDTO patientOnlineOrderDTO){
        if(patientOnlineOrderDTO.getPageNum() != null && patientOnlineOrderDTO.getPageSize() != null){
            PageHelper.startPage(patientOnlineOrderDTO.getPageNum(), patientOnlineOrderDTO.getPageSize());
        }
        Example example = new Example(PatientOnlineOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(patientOnlineOrderDTO.getStatus() != null){
            criteria.andEqualTo("status", patientOnlineOrderDTO.getStatus());
        }else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(patientOnlineOrderDTO.getDoctorId() != null){
            criteria.andEqualTo("doctorId", patientOnlineOrderDTO.getDoctorId());
        }
        if(patientOnlineOrderDTO.getPatientUserId() != null){
            criteria.andEqualTo("patientUserId", patientOnlineOrderDTO.getPatientUserId());
        }
        if(patientOnlineOrderDTO.getBizStatus() != null){
            criteria.andEqualTo("bizStatus", patientOnlineOrderDTO.getBizStatus());
        }
        if(patientOnlineOrderDTO.getPayStatus() != null){
            criteria.andEqualTo("payStatus", patientOnlineOrderDTO.getPayStatus());
        }
        if(patientOnlineOrderDTO.getStartTime() != null){
            criteria.andGreaterThanOrEqualTo("createdTime", patientOnlineOrderDTO.getStartTime());
        }
        if(patientOnlineOrderDTO.getEndTime() != null){
            criteria.andLessThanOrEqualTo("createdTime", patientOnlineOrderDTO.getEndTime());
        }
        example.setOrderByClause(" created_time desc");
        List<PatientOnlineOrder> list = patientOnlineOrderMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public int listCount(PatientOnlineOrderDTO patientOnlineOrderDTO){
        Example example = new Example(PatientOnlineOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(patientOnlineOrderDTO.getStatus() != null){
            criteria.andEqualTo("status", patientOnlineOrderDTO.getStatus());
        }else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(patientOnlineOrderDTO.getDoctorId() != null){
            criteria.andEqualTo("doctorId", patientOnlineOrderDTO.getDoctorId());
        }
        return patientOnlineOrderMapper.selectCountByExample(example);
    }

    public List<PatientOnlineOrder> activeOnlineOrder(){
        Example example = new Example(PatientOnlineOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("patientUserId", ReqComHold.getReqCom().getUserId());
        criteria.andLessThan("bizStatus", OnlineBizStatusEnum.COMPLETE.getCode());
        return patientOnlineOrderMapper.selectByExample(example);
    }

    public PatientOnlineOrder lastOnlineOrder(){
        Example example = new Example(PatientOnlineOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("patientUserId", ReqComHold.getReqCom().getUserId());
        example.setOrderByClause(" created_time desc");
        List<PatientOnlineOrder> onlineOrders = patientOnlineOrderMapper.selectByExample(example);
        return CollectionUtils.isEmpty(onlineOrders) ? null : onlineOrders.get(0);
    }


    public List<PatientOnlineOrder> select(PatientOnlineOrder patientOnlineOrder){
        return patientOnlineOrderMapper.select(patientOnlineOrder);
    }

    public int insert(PatientOnlineOrder patientOnlineOrder){
        patientOnlineOrder.setId(SystemUtil.getId());
        patientOnlineOrder.setStatus(CommonStatusEnum.INIT.getCode());
        patientOnlineOrder.setCreatedTime(new Date());
        return patientOnlineOrderMapper.insert(patientOnlineOrder);
    }

    public int update(PatientOnlineOrder patientOnlineOrder){
        patientOnlineOrder.setUpdateTime(new Date());
        return patientOnlineOrderMapper.updateByPrimaryKeySelective(patientOnlineOrder);
    }

    public PatientOnlineOrder findById(Long id){
        return patientOnlineOrderMapper.selectByPrimaryKey(id);
    }

    public List<PatientOnlineOrder> findByIds(List<Long> ids){
        Example example = new Example(PatientOnlineOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return patientOnlineOrderMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(PatientOnlineOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        PatientOnlineOrder patientOnlineOrder = new PatientOnlineOrder();
        patientOnlineOrder.setStatus(CommonStatusEnum.DEL.getCode());
        return patientOnlineOrderMapper.updateByExampleSelective(patientOnlineOrder, example);
    }

}
